package com.wskj.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.wskj.common.core.page.PagePlus;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.utils.common.AssertUtils;
import com.wskj.common.utils.common.PageUtils;
import com.wskj.system.bo.SysVipConfigAddBo;
import com.wskj.system.bo.SysVipConfigEditBo;
import com.wskj.system.bo.SysVipConfigQueryBo;
import com.wskj.system.domain.SysVipConfig;
import com.wskj.system.mapper.SysVipConfigMapper;
import com.wskj.system.service.ISysVipConfigService;
import com.wskj.system.vo.app.AppSysVipConfigVo;
import com.wskj.system.vo.web.SysVipConfigVo;
import org.springframework.stereotype.Service;
import com.wskj.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会员卡配置Service业务层处理
 *
 * @author wsdz
 * @date 2023-03-06
 */
@Service
public class SysVipConfigServiceImpl extends ServicePlusImpl<SysVipConfigMapper, SysVipConfig> implements ISysVipConfigService {

    @Override
    public SysVipConfigVo queryById(Long id){
        return getVoById(id, SysVipConfigVo.class);
    }

    @Override
    public TableDataInfo<SysVipConfigVo> queryPageList(SysVipConfigQueryBo bo) {
        PagePlus<SysVipConfig, SysVipConfigVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysVipConfigVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysVipConfigVo> queryList(SysVipConfigQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysVipConfigVo.class);
    }

    private LambdaQueryWrapper<SysVipConfig> buildQueryWrapper(SysVipConfigQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysVipConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), SysVipConfig::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getCoverPic()), SysVipConfig::getCoverPic, bo.getCoverPic());
        lqw.eq(StrUtil.isNotBlank(bo.getCarouselPic()), SysVipConfig::getCarouselPic, bo.getCarouselPic());
        lqw.eq(bo.getType() != null, SysVipConfig::getType, bo.getType());
        lqw.like(StrUtil.isNotBlank(bo.getTypeName()), SysVipConfig::getTypeName, bo.getTypeName());
        lqw.eq(bo.getDays() != null, SysVipConfig::getDays, bo.getDays());
        lqw.eq(bo.getPrice() != null, SysVipConfig::getPrice, bo.getPrice());
        lqw.eq(StrUtil.isNotBlank(bo.getBrief()), SysVipConfig::getBrief, bo.getBrief());
        lqw.eq(StrUtil.isNotBlank(bo.getBriefInfo()), SysVipConfig::getBriefInfo, bo.getBriefInfo());
        lqw.eq(bo.getStatus() != null, SysVipConfig::getStatus, bo.getStatus());
        lqw.eq(bo.getSort() != null, SysVipConfig::getSort, bo.getSort());
        lqw.orderByAsc(SysVipConfig::getSort);
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysVipConfigAddBo bo) {
        SysVipConfig add = BeanUtil.toBean(bo, SysVipConfig.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysVipConfigEditBo bo) {
        SysVipConfig update = BeanUtil.toBean(bo, SysVipConfig.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysVipConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importSysVipConfig(List<SysVipConfigVo> sysVipConfigList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(sysVipConfigList), "导入数据不能为空");
        int successNum = 0;
        for (SysVipConfigVo sysVipConfig : sysVipConfigList) {
            SysVipConfig add = BeanUtil.toBean(sysVipConfig, SysVipConfig.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", sysVipConfigList.size(), successNum);
        if (successNum != sysVipConfigList.size()) {
            msg = msg + String.format("失败 %d 条！", sysVipConfigList.size() - successNum);
        }
        return msg;
    }

    @Override
    public TableDataInfo<AppSysVipConfigVo> pageList(SysVipConfigQueryBo bo) {
        PagePlus<SysVipConfig, AppSysVipConfigVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), AppSysVipConfigVo.class);
        return PageUtils.buildDataInfo(result);
    }
}
